<?php
namespace app\teacher\controller;
use app\common\Base;
use app\common\TeacherModel;
use app\common\Excel;
use think\Db;

class TeacherManage extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function update() {
		$ID = request()->param("id");
		if($ID > 0) {
			$where["ID"] = $ID;
			$result = Db::table("教师信息")->where($where)->find();
			$result = findResult($result);
			$gradeKey = "";
			if(array_key_exists("年级号", $result) && $result["年级号"] > 0) {
				$gradeKey = $result["年级号"]."_".$result["学籍类型"];
			}
			$result["gradeKey"] = $gradeKey;
			$this->assign("detail", $result);
		}else {
			$this->assign("detail", []);
		}
		return $this->fetch();
	}

	public function pageQuery() {
		$where = [];
		$grade = input("grade");
		$subject = input("subjectId");
		$name = input("teacherName");
		$page = input("page");
		$rows = input("rows");
		if($grade) {
			$array = explode("_", $grade);
			$where["年级号"] = array("eq", $array[0]);
			$where["学籍类型"] = array("eq", $array[1]);
		}
		if($subject) $where["任教学科ID"] = array("eq", $subject);
		if($name) $where["姓名"] = array("like", $name."%");
		$fields = [];
		$Model = new TeacherModel();
		return $Model->getTeacherList($page, $rows, $fields, $where);
	}

	public function exportExcel() {
		$where = [];
		$grade = input("grade");
		$subject = input("subjectId");
		$name = input("teacherName");
		if($grade) {
			$array = explode("_", $grade);
			$where["年级号"] = array("eq", $array[0]);
			$where["学籍类型"] = array("eq", $array[1]);
		}
		if($subject) $where["任教学科ID"] = array("eq", $subject);
		if($name) $where["姓名"] = array("like", $name."%");
		$fields = [];
		$Model = new TeacherModel();
		$result = $Model->getTeacherList(1, PHP_INT_MAX, $fields, $where);  
		//excel表头内容
		$header = array('姓名'=>'姓名','年级号'=>'gradeName','任教学科'=>'subjectName','手机号'=>'手机号','性别'=>'性别','身份证号'=>'身份证号','出生日期'=>'出生日期','职称'=>'职称','籍贯'=>'籍贯','民族'=>'民族','政治面貌'=>'政治面貌','居住地址'=>'居住地址','入党团时间'=>'入党团时间','户口性质'=>'户口性质','婚姻状况'=>'婚姻状况','毕业参加工作时间'=>'毕业参加工作时间','大学专业'=>'大学专业','是否正编教师'=>'是否正编教师','是否有普通话证'=>'是否有普通话证','是否具备心理ABC证'=>'是否具备心理ABC证','其他获得资格证书'=>'其他获得资格证书');
		Excel::toExcel($result["rows"],'教师信息',$header);
		exit;
	}

	public function addUpdate() {
		$all = request()->param();
		try {
			if(array_key_exists("年级号", $all) && !empty($all["年级号"])) {
				if($all["年级号"] == "0") {
					$all["年级号"] = 0;
				}else {
					$array = explode("_", $all["年级号"]);
					$all["年级号"] = $array[0];
					$all["学籍类型"] = $array[1];
				}
			}
			if(array_key_exists("职位", $all) && !empty($all["职位"])) {
				$data = Db::table("t_organize_depart")->where("PARENT_ID > 0")->select();
				$array = explode(",", $all["职位"]);
			    $result = [];
			    foreach ($array as $value) {
			        foreach ($data as $obj) {
			        	if($obj["ID"] == $value && !in_array($obj["PARENT_ID"], $result)) {
							array_push($result, $obj["PARENT_ID"]);
							break;
			        	}
			        }
			    }
			    if(count($result) > 0) {
			    	$all["部门"] = implode(",", $result);
			    }
			}
			if(array_key_exists("ID", $all)) {
				update('教师信息', $all);
			} else {
				insert('教师信息', $all);
			}
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

	public function del() {
		$delIds = input("delIds");
		try {
			Db::table("教师信息")->delete(explode(",", $delIds));
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

	function importExcel() {
	    //上传excel文件
	    $file = request()->file('files');
	    if(!$file) {
	    	return "请选择文件";
	    }
	    $data = Excel::importExcel($file);
	    if(count($data) == 0) {
	    	return "没有数据！";
	    }
	    try {
    		$errorList = [];
    		$resultList = [];
    		foreach ($data as $idx => $obj) {
    			$result["姓名"] = $obj["姓名"];
    			$result["性别"] = $obj["性别"];
    			$result["手机号"] = $obj["联系号码"];
    			$result["身份证号"] = $obj["身份证号"];
    			$result["居住地址"] = $obj["家庭住址"];
    			if(!empty($obj["职位"])) {
    				$pl = orgPositionList();
    				$array = explode("、", $obj["职位"]);
				    $dp = [];
				    $pn = [];
				    foreach ($array as $value) {
				        foreach ($pl as $plb) {
				        	if($plb["POSITION"] == $value) {
				        		array_push($pn, $plb["ID"]);
				        		if(!in_array($plb["PARENT_ID"], $dp)) {
									array_push($dp, $plb["PARENT_ID"]);
								}
								break;
				        	}
				        }
				    }
				    if(count($dp) > 0) {
				    	$result["部门"] = implode(",", $dp);
				    }
				    if(count($pn) > 0) {
				    	$result["职位"] = implode(",", $pn);
				    }
    			} 
    			if (!empty($obj["任教科目"])) {
    				$sl = subjectMap();
    				foreach ($sl as $key => $value) {
						if($value == $obj["任教科目"]) {
							$result["任教学科ID"] = $key;
							break;
						}
					}
    			} 
    			if (!empty($obj["任课年级"])) {
    				$gl = gradeMap();
    				foreach ($gl as $key => $value) {
						if($value == $obj["任课年级"]) {
							$array = explode("_", $key);
							$result["年级号"] = $array[0];
    						$result["学籍类型"] = $array[1];
							break;
						}
					}
    			}
    			$resultList[$idx] = $result;
    		}
			Db::table('教师信息')->insertAll($resultList);
	    } catch (\Exception $e) {
	    	return $e->getMessage();
	    }
	    return "000";
	}

}
